package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.PriceSetting;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class PriceListAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;

    public List<PriceSetting> list;

    public String unit = "";
    public String unitPackage = "";


    private AdapterView.OnItemClickListener onItemClickListener;

    public PriceListAdapter(Context context, List<PriceSetting> list) {
        this.context = context;
        this.list = list;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getUnitPackage() {
        return unitPackage;
    }

    public void setUnitPackage(String unitPackage) {
        this.unitPackage = unitPackage;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_price_setting, null, false);

            vh.tvCity = (TextView) convertView.findViewById(R.id.tv_city);
            vh.tvDelivery = (TextView) convertView.findViewById(R.id.tv_delivery);
            vh.tvPickUp = (TextView) convertView.findViewById(R.id.tv_pick_up);
            vh.tvOnline = (TextView) convertView.findViewById(R.id.tv_online);
            vh.tvOffline = (TextView) convertView.findViewById(R.id.tv_offline);
            vh.tvUnit = (TextView) convertView.findViewById(R.id.tv_inventory);
            vh.tvUnitPackage = (TextView) convertView.findViewById(R.id.tv_package);

            vh.edPrice = (EditText) convertView.findViewById(R.id.tv_moq);
            vh.edUnit = (EditText) convertView.findViewById(R.id.ed_amount);

            vh.ivSelector = (ImageView) convertView.findViewById(R.id.iv_selector);
            vh.ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            vh.ivRemove = (ImageView) convertView.findViewById(R.id.iv_remove);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        vh.ivSelector.setTag(R.id.item_key_position, position);
        vh.ivSelector.setOnClickListener(this);
        vh.tvCity.setTag(R.id.item_key_position, position);
        vh.tvCity.setTag(R.id.item_key_view, vh.ivSelector);
        vh.tvCity.setOnClickListener(this);

        vh.tvDelivery.setTag(R.id.item_key_position, position);
        vh.tvDelivery.setOnClickListener(this);

        vh.tvPickUp.setTag(R.id.item_key_position, position);
        vh.tvPickUp.setOnClickListener(this);

        vh.tvOnline.setTag(R.id.item_key_position, position);
        vh.tvOnline.setOnClickListener(this);

        vh.tvOffline.setTag(R.id.item_key_position, position);
        vh.tvOffline.setOnClickListener(this);

        vh.ivAdd.setTag(R.id.item_key_position, position);
        vh.ivAdd.setTag(R.id.item_key_view, vh.edPrice);
        vh.ivAdd.setOnClickListener(this);

        vh.ivRemove.setTag(R.id.item_key_position, position);
        vh.ivRemove.setTag(R.id.item_key_view, vh.edPrice);
        vh.ivRemove.setOnClickListener(this);

        vh.edPrice.setTag(R.id.item_key_position, position);
        vh.edPrice.addTextChangedListener(new EdTextWatch(position, vh.edPrice));
        vh.edUnit.setTag(R.id.item_key_position, position);
        vh.edUnit.addTextChangedListener(new EdUnitWatch(position, vh.edPrice));


        if (onItemClickListener != null) {
            convertView.setTag(R.id.item_key_position, position);
            convertView.setOnClickListener(this);
        }

        PriceSetting data = list.get(position);
        updateTag(vh.tvDelivery, data.getStatusDeliverySelect());
        updateTag(vh.tvPickUp, data.getStatusPickUpSelect());
        updateTag(vh.tvOnline, data.getStatusOnlineSelect());
        updateTag(vh.tvOffline, data.getStatusOfflineSelect());


        if (data.isSelect()) {

            vh.ivSelector.setImageResource(R.mipmap.select_round);
        } else {
            vh.ivSelector.setImageResource(R.mipmap.unselect_round);

        }
        vh.edPrice.setText(String.format("%.2f", data.getPrice()));

        vh.tvUnit.setText(unit);
        vh.tvUnitPackage.setText(unitPackage);
        vh.edUnit.setText(data.getMoq());
        vh.tvCity.setText(data.getCityName());

        return convertView;
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            int position = (int) o;
            switch (v.getId()) {
                case R.id.rl_price_setting:
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(null, v, position, position);
                    }
                    break;
                case R.id.iv_selector:
                    PriceSetting priceSetting = list.get(position);
                    priceSetting.setSelect(!priceSetting.isSelect());
                    if (priceSetting.isSelect()) {
                        ((ImageView) v).setImageResource(R.mipmap.select_round);
                    } else {
                        ((ImageView) v).setImageResource(R.mipmap.unselect_round);
                    }
                    break;
                case R.id.tv_city:
                    PriceSetting priceSetting111 = list.get(position);
                    priceSetting111.setSelect(!priceSetting111.isSelect());
                    if (priceSetting111.isSelect()) {
                        ((ImageView) v.getTag(R.id.item_key_view)).setImageResource(R.mipmap.select_round);
                    } else {
                        ((ImageView) v.getTag(R.id.item_key_view)).setImageResource(R.mipmap.unselect_round);
                    }
                    break;
                case R.id.iv_add:
                    updatePrice(1f, 1, (EditText) v.getTag(R.id.item_key_view));
                    break;

                case R.id.iv_remove:
                    updatePrice(1f, -1, (EditText) v.getTag(R.id.item_key_view));

                    break;
                case R.id.tv_delivery:
                    PriceSetting priceSetting1 = list.get(position);
                    int status = priceSetting1.getStatusDeliverySelect();
                    if (status == 0) {
                        priceSetting1.setStatusDeliverySelect(1);
                        updateTag((TextView) v, 1);
                    } else if (status > 0) {
                        priceSetting1.setStatusDeliverySelect(0);
                        updateTag((TextView) v, 0);
                    }
                    break;

                case R.id.tv_pick_up:
                    PriceSetting priceSetting2 = list.get(position);
                    int status2 = priceSetting2.getStatusPickUpSelect();
                    if (status2 == 0) {
                        priceSetting2.setStatusPickUpSelect(1);
                        updateTag((TextView) v, 1);
                    } else if (status2 > 0) {
                        priceSetting2.setStatusPickUpSelect(0);
                        updateTag((TextView) v, 0);
                    }
                    break;

                case R.id.tv_online:
                    PriceSetting priceSetting23 = list.get(position);
                    int status23 = priceSetting23.getStatusOnlineSelect();
                    if (status23 == 0) {
                        priceSetting23.setStatusOnlineSelect(1);
                        updateTag((TextView) v, 1);
                    } else if (status23 > 0) {
                        priceSetting23.setStatusOnlineSelect(0);
                        updateTag((TextView) v, 0);
                    }
                    break;

                case R.id.tv_offline:
                    PriceSetting priceSetting24 = list.get(position);
                    int status24 = priceSetting24.getStatusOfflineSelect();
                    if (status24 == 0) {
                        priceSetting24.setStatusOfflineSelect(1);
                        updateTag((TextView) v, 1);
                    } else if (status24 > 0) {
                        priceSetting24.setStatusOfflineSelect(0);
                        updateTag((TextView) v, 0);
                    }
                    break;


            }
        }
    }

    public void updateTag(TextView tv, int status) {
        if (status == 0) {//未选中
            tv.setBackgroundResource(R.drawable.tag_unselect_bg);
            tv.setTextColor(Color.parseColor("#434343"));
        } else if (status > 0) {
            tv.setBackgroundResource(R.drawable.tag_select_bg);
            tv.setTextColor(Color.parseColor("#f43b00"));
        } else {
            tv.setBackgroundResource(R.drawable.tag_unable_bg);
            tv.setTextColor(Color.parseColor("#b5b5b5"));
        }
    }

    public void updatePrice(float dff, int mode, EditText ed) {

        String s = ed.getText().toString();
        if (TextUtils.isEmpty(s)) {
            s = "0.00";
        }
        float price = Float.valueOf(s);
        if (mode > 0) {
            price += dff;
        } else {
            price -= dff;
            price = price < 0 ? 0 : price;
        }
        ed.setText(String.format("%.2f", price));
    }

    public class EdTextWatch implements TextWatcher {

        int position;
        EditText ed;

        public EdTextWatch(int position, EditText ed) {
            this.position = position;
            this.ed = ed;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //删除“.”后面超过2位后的数据
            if (s.toString().contains(".")) {
                if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                    s = s.toString().subSequence(0,
                            s.toString().indexOf(".") + 3);
                    ed.setText(s);
                    ed.setSelection(s.length()); //光标移到最后
                }
            }
            //如果"."在起始位置,则起始位置自动补0
            if (s.toString().trim().substring(0).equals(".")) {
                s = "0" + s;
                ed.setText(s);
                ed.setSelection(2);
            }
            //如果起始位置为0,且第二位跟的不是".",则无法后续输入
            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    ed.setText(s.subSequence(0, 1));
                    ed.setSelection(1);
                    return;
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString().trim()) && list.size() > position) {
                list.get(position).setPrice(Float.valueOf(s.toString().trim()));
            } else if (list.size() > position) {
                list.get(position).setPrice(0);
            }
        }
    }

    public class EdUnitWatch implements TextWatcher {

        int position;
        EditText ed;

        public EdUnitWatch(int position, EditText ed) {
            this.position = position;
            this.ed = ed;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (list.size() > position)
                list.get(position).setMoq(s.toString().trim());
        }
    }

    public class ViewHolder {

        ImageView ivSelector;
        ImageView ivAdd;
        ImageView ivRemove;
        TextView tvCity;
        TextView tvDelivery;
        TextView tvPickUp;
        TextView tvOnline;
        TextView tvOffline;
        TextView tvUnit;
        TextView tvUnitPackage;
        EditText edPrice;
        EditText edUnit;

    }
}
